#include				"Order.h"
#include				<iostream>

Order::Order() : _content(NULL), _sender_id(-1), _current(0)
{

}

Order::~Order()
{
	if (_content != NULL)
		delete _content;
}

Protocol				Order::getProtocol() const
{
	return _protocol;
}

void 					*Order::getData(size_t size)
{
	byte				*data = new byte[size];
	size_t				cnt = 0;
	size_t				total = _content->size();

	while (_current < total && cnt < size)
	{
		data[cnt] = (*_content)[_current];
		_current++;
		cnt++;
	}

	if (cnt != size)
		return NULL;
	return data;
}

byte					*Order::send()
{
	unsigned short		size = _content->size();
	std::vector<byte>::iterator it = _content->begin();
	byte				*msg = new byte[size + sizeof(size)];
	size_t				cnt = 2;

	msg[0] =  ((byte*)&size)[0];
	msg[1] =  ((byte*)&size)[1];
	while (it != _content->end())
	{
		msg[cnt] = *it;
		cnt++;
		it++;
	}
	return msg;
}

unsigned short			Order::getSenderId() const
{
	return _sender_id;
}

size_t					Order::size() const
{
	return _content->size();
}

void					Order::pushData(void *data, size_t size)
{
	byte				*input = (byte*)data;

	for (size_t cnt = 0; cnt < size; ++cnt)
	_content->push_back(input[cnt]);
}

void					Order::setSenderId(unsigned short id)
{
	_sender_id = id;
}

void					Order::setData(std::vector<byte> *data)
{
	_content = data;
	_current = 0;
}

void					Order::init()
{
	if (_content != NULL)
		delete _content;
	_content = new std::vector<byte>;
}